let tinify = require('tinify');
// require("tinify").key = 'qoooKRe3mSFLar1619PxiTVDtaiwTHva';

export async function doTiny(key: string, buffer: Buffer) {
    buffer = Buffer.from(buffer);
    return new Promise<{ data: Buffer, count: number }>(function (resolve, reject) {
        tinify.key = key;
        var ty = tinify.fromBuffer(buffer);
        try {
            ty.toBuffer((reason, data: Buffer) => {
                if (reason) {
                    reject(reason);
                }
                else {
                    resolve({ data: data, count: tinify.compressionCount });
                }
            });
        }
        catch (e) {
            reject(e)
        }
    })
}

process.on("message", async function (m) {
    try {
        let data = JSON.parse(m.toString());
        let res = await doTiny(data.key, data.info);
        process.send(JSON.stringify(res));
    }
    catch (e) {
        process.stderr.push(e);
    }
})

setInterval(() => {
}, 1000);